#coding:utf-8
from modules.decorators import MaybeCallInThread
from modules import utils
import weakref
import os

class roomItem(dict):
        __slots__ = ("conference", "config" ,"botCore")
        def __init__(self,botCore,conference):
                super(roomItem,self).__init__()
                self.conference = conference
                self.botCore = weakref.proxy(botCore)
                self.config = None
                self.loadConfig()
                
        def __str__(self):
                return self.conference
        
        def __unicode__(self):
                return self.conference
                
        @MaybeCallInThread
        def loadConfig(self):
                self.config = utils.loadConfig(os.path.join("configs",self.conference+".ini"),{
                                "nick":self.botCore.profile.NICK,
                                "password":"",
                                "status":"dnd",
                                "show":"online",
                                "priority":0,
                                "accesses":{},
                                "modulesconfig":{},
                                "cmdacces":{}})
                                
        @MaybeCallInThread
        def reloadConfig(self):
                self.config.reload()
                
        @MaybeCallInThread
        def saveConfig(self):
                self.copyConfig().write()
                
        def sendMsg(self,body):
                self.botCore.sendMsg("groupchat",self.conference,body)
                
        def getOnlineUsers(self):
                return [ nick for nick,userItem in self.iteritems() if userItem.isHere ]
                
        def getBotNick(self):
                return self.config["nick"]
                
        def getTueJID(self,nick):
                return self[nick].jid
                
        def getLocalAccess(self,jid):
                if self.config["accesses"].has_key(jid):
                        return self.config["accesses"].as_int(jid)
                return None
                
        def getGlobalAccess(self,jid):
                return self.botCore.getGlobalAccess(jid)
                
        def modifyAccess(self,jid,permAccess = None):
                return self.getGlobalAccess(jid) or self.getLocalAccess(jid) or permAccess
                
        def MUCQuery(self,itemType,user,mucType,stat,reason = None):
                iq = self.botCore.IQ("set",self.conference)
                query = iq.addElement("query","http://jabber.org/protocol/muc#admin")
                item = query.addElement("item")
                item[itemType] = user
                item[mucType] = status
                self.client.send(iq)
                return iq
                
        def join(self):
                self.botCore.joinConference(self.conference,self.config["nick"],
                        self.config["priority"],self.config["status"],self.config["show"],self.config["password"])
                
        def copyConfig(self):
                import copy
                return copy.copy(self.config)
              
